package com.zyx.mbg;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.internal.DefaultCommentGenerator;
import org.mybatis.generator.internal.util.StringUtility;

import java.util.Properties;
import java.util.Set;

/**
 * 自定义注释生成器
 * Created by macro on 2018/4/26.
 */
public class CommentGenerator extends DefaultCommentGenerator {
    private final Properties properties;

    public CommentGenerator() {
        properties = new Properties();
    }

    public CommentGenerator(Properties properties) {
        this.properties = properties;
    }

    @Override
    public void addConfigurationProperties(Properties properties) {
        // 获取自定义的 properties
        this.properties.putAll(properties);
    }

    @Override
    public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        String author = properties.getProperty("author", "zyx");
        String authorDesc = " * @author " + author;
        String sinceDesc = " * @since " + DateUtils.getDateStr();
        String tableDesc;
        // 获取表注释
        if (StringUtility.stringHasValue(introspectedTable.getRemarks())) {
            topLevelClass.addImportedType("lombok.Data");
            topLevelClass.addJavaDocLine("/**");
            tableDesc = " * <p>对应表: " +
                    introspectedTable.getFullyQualifiedTable() +
                    "【" + introspectedTable.getRemarks() + "】" +
                    "</p>";
            topLevelClass.addJavaDocLine(tableDesc);
            topLevelClass.addJavaDocLine(" *");
            topLevelClass.addJavaDocLine(authorDesc);
            topLevelClass.addJavaDocLine(sinceDesc);
            topLevelClass.addJavaDocLine(" */");
            topLevelClass.addAnnotation("@Data");
        } else {
            topLevelClass.addImportedType("lombok.Data");
            topLevelClass.addJavaDocLine("/**");
            tableDesc = " * <p>对应表: " +
                    introspectedTable.getFullyQualifiedTable() +
                    "</p>";
            topLevelClass.addJavaDocLine(tableDesc);
            topLevelClass.addJavaDocLine(" *");
            topLevelClass.addJavaDocLine(authorDesc);
            topLevelClass.addJavaDocLine(sinceDesc);
            topLevelClass.addJavaDocLine(" */");
            topLevelClass.addAnnotation("@Data");
        }
    }

    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) {
            field.addJavaDocLine("/**");
            field.addJavaDocLine(" * " + introspectedColumn.getRemarks());
            field.addJavaDocLine(" */");
        }
    }

    @Override
    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {

    }

    @Override
    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean b) {

    }

    @Override
    public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {

    }

    @Override
    public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {

    }

    @Override
    public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {

    }

    @Override
    public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {

    }

    @Override
    public void addJavaFileComment(CompilationUnit compilationUnit) {

    }

    @Override
    public void addComment(XmlElement xmlElement) {

    }

    @Override
    public void addRootComment(XmlElement xmlElement) {

    }

    @Override
    public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> set) {

    }

    @Override
    public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> set) {

    }

    @Override
    public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> set) {

    }

    @Override
    public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> set) {

    }

    @Override
    public void addClassAnnotation(InnerClass innerClass, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> set) {

    }

    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable) {

    }

}
